﻿using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using NETCONLib;
using WirelessAccessPoint.Core;
using WirelessAccessPoint.Resources;

namespace WirelessAccessPoint.Model
{
    /// <summary>
    /// Internet Connection Sharing (ICS)を表します。
    /// </summary>
    public class NetSharing : ModelBase
    {
        private string guid;
        private string name;
        private string deviceName;
        private uint? characteristics;
        private tagNETCON_MEDIATYPE? mediaType;
        private tagNETCON_STATUS? status;
        private bool? sharingEnabled;
        private tagSHARINGCONNECTIONTYPE? sharingConnectionType;
        private IPAddress[] ipAddresses;

        #region Object メンバ

        public override bool Equals(object obj)
        {
            return this.Equals(obj as NetSharing);
        }

        public bool Equals(NetSharing other)
        {
            return other == null ? false : this.GUID == other.GUID;
        }

        public override int GetHashCode()
        {
            return this.GUID == null ? 0 : this.GUID.GetHashCode();
        }

        #endregion

        #region プロパティ

        /// <summary>
        /// GUIDを設定または取得します。
        /// </summary>
        public string GUID
        {
            get { return this.guid; }
            set
            {
                if (value != this.guid)
                {
                    this.guid = value;
                    this.OnPropertyChanged(() => this.GUID);
                }
            }
        }

        /// <summary>
        /// 名前を設定または取得します。
        /// </summary>
        public string Name
        {
            get { return this.name; }
            set
            {
                if (value != this.name)
                {
                    this.name = value;
                    this.OnPropertyChanged(() => this.Name);
                }
            }
        }

        /// <summary>
        /// デバイス名を設定または取得します。
        /// </summary>
        public string DeviceName
        {
            get { return this.deviceName; }
            set
            {
                if (value != this.deviceName)
                {
                    this.deviceName = value;
                    this.OnPropertyChanged(() => this.DeviceName);
                }
            }
        }

        public uint? Characteristics
        {
            get { return this.characteristics; }
            set
            {
                if (value != this.characteristics)
                {
                    this.characteristics = value;
                    this.OnPropertyChanged(() => this.Characteristics);
                }
            }
        }

        /// <summary>
        /// 通信方式を設定または取得します。
        /// </summary>
        public tagNETCON_MEDIATYPE? MediaType
        {
            get { return this.mediaType; }
            set
            {
                if (value != this.mediaType)
                {
                    this.mediaType = value;
                    this.OnPropertyChanged(() => this.MediaType);
                    this.OnPropertyChanged(() => this.MediaTypeString);
                }
            }
        }

        /// <summary>
        /// 状態を設定または取得します。
        /// </summary>
        public tagNETCON_STATUS? Status
        {
            get { return this.status; }
            set
            {
                if (value != this.status)
                {
                    this.status = value;
                    this.OnPropertyChanged(() => this.Status);
                    this.OnPropertyChanged(() => this.StatusString);
                }
            }
        }

        /// <summary>
        /// 有効/無効を設定または取得します。
        /// </summary>
        public bool? SharingEnabled
        {
            get { return this.sharingEnabled; }
            set
            {
                if (value != this.sharingEnabled)
                {
                    this.sharingEnabled = value;
                    this.OnPropertyChanged(() => this.SharingEnabled);
                }
            }
        }

        /// <summary>
        /// Public側/Private側を設定または取得します。
        /// </summary>
        public tagSHARINGCONNECTIONTYPE? SharingConnectionType
        {
            get { return this.sharingConnectionType; }
            set
            {
                if (value != this.sharingConnectionType)
                {
                    this.sharingConnectionType = value;
                    this.OnPropertyChanged(() => SharingConnectionType);
                }
            }
        }

        /// <summary>
        /// IPアドレスを設定または取得します。
        /// </summary>
        public IPAddress[] IPAddresses
        {
            get { return this.ipAddresses; }
            set
            {
                if (value != this.ipAddresses)
                {
                    this.ipAddresses = value;
                    this.OnPropertyChanged(() => IPAddresses);
                    this.OnPropertyChanged(() => IPAddressesString);
                }
            }
        }


        #endregion

        #region 表示用プロパティ

        /// <summary>
        /// 通信方式の文字列表現を取得します。
        /// </summary>
        public string MediaTypeString
        {
            get { return this.MediaType.ToLabel<NetconMediaTypeResource>(); }
        }

        /// <summary>
        /// 状態の文字列表現を取得します。
        /// </summary>
        public string StatusString
        {
            get { return this.Status.ToLabel<NetconStatusResource>(); }
        }

        /// <summary>
        /// IPアドレスの文字列表現を取得します。
        /// </summary>
        public string IPAddressesString
        {
            get { return string.Join("\n", this.IPAddresses.Select(i => i.ToString())); }
        }

        #endregion
    }
}
